﻿//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
* 文档信息插件类；
*/



define(function (require, exports, module) {


    FXREADER.CDocPropertiesPlugin = function (pReaderApp) {
        var _DocInfoDlgHtml = "",
              _pReaderApp = pReaderApp,
              _bDlgInited = false,
              _PopupDlg = null,
              _Language = _pReaderApp.GetLanguage(),
              _self = this;

        var _textID = {
            author: "atrtxt",
            creator: "ctrtxt",
            producer: "pdrtxt",
            title: "ttltxt",
            subject: "subtxt",
            keywords: "kwdtxt",
            creationData: "ctdtxt",
            modData: "mddtxt",
            version: "vertxt",
            name: "nametxt",
            pagecount: "cnttxt",
            pagesize: "sizetxt",
            unit: "unit"
        };

        //private
        function _ChangeUnit(unit, value) {
            var t_value = FXREADER.Common.ChangeUnit(value, "point", unit, 0);
            return FXREADER.Common.change2Decimal_f(t_value, 2);
        }

        //获取Plugin名称;
        this.GetPluginName = function () {
            return "DocProperties Plugin";
        }
        //插件被注册的时候调用;
        this.PIOnRegister = function () {
           
        }
        //模块初始化;
        this.PIInit = function () {
            _self.CreateCmdAndBtn();
            _pReaderApp.StepProgress();
        }
        //模块卸载;
        this.PIUnload = function () {
        }

        this.GetReaderApp = function () {
            return _pReaderApp;
        }

        this.PopupPropertiesDlg = function () {
            var pDocProperties = _pReaderApp.GetPDFDoc().GetDocProperties();
            if (pDocProperties == null)
                return;
            if (!_bDlgInited) {
                _DocInfoDlgHtml = "<div id='fwr_doc_properties_dlg' class='fwr_hidden'>" +
                                                    "<div class='fwr_dialog fwr_dialog_padding dn ui-dialog-content ui-widget-content' style='display: block;width: auto; min-height: 90px;'>" +
                                                            "<div style='height:auto'>" +
                                                                "<div class='content'>" +
                                                                    "<div class='sp'><span class='left'>" + _Language.DocProperties.PDFTitle + "</span><span id='ttltxt' class='txtspan' title=''></span></div>" +
                                                                    "<div class='sp'><span class='left'>" + _Language.DocProperties.Subject + "</span><span id='subtxt' class='txtspan'></span></div>" +
                                                                    "<div class='sp'><span class='left'>"+_Language.DocProperties.Author+"</span><span id='atrtxt' class='txtspan'></span></div>" +
                                                                    "<div class='sp'><span class='left'>"+_Language.DocProperties.Creator+"</span><span id='ctrtxt' class='txtspan'></span></div>" +
                                                                    "<div class='sp'><span class='left'>" + _Language.DocProperties.Producer + "</span><span id='pdrtxt' class='txtspan'></span></div>" +
                                                                    "<div class='sp'><span class='left' style='float:left'>" + _Language.DocProperties.Keywords + "</span><textarea id='kwdtxt' style='width:272px;max-width:272px;height:55px; resize:none;' class='fwr_doc_properties_dlg_keyword' disabled='disabled'></textarea></div>" +
                                                                    "<div class='sp'><span class='left'>"+_Language.DocProperties.CreateDate+"</span><span  id='ctdtxt' class='txtspan'></span></div>" +
                                                                    "<div class='sp'><span class='left'>" + _Language.DocProperties.ModifyDate + "</span><span  id='mddtxt' class='txtspan'></span></div>" +
                                                                    "<div class='separator'></div>" +
                                                                    "<div class='sp'><span class='left'>"+_Language.DocProperties.PDFVersion+"</span><span id='vertxt'></span></div>" +
                                                                    "<div class='sp'><span class='left'>"+_Language.DocProperties.PageSize+"</span><span id='sizetxt'></span>" +
                                                                        "<select id='unit' style='width:53px;margin-left:4px'>" +
                                                                            "<option value='inch'>" + _Language.DocProperties.Inch + "</option>" +
                                                                            "<option value='cm'>" + _Language.DocProperties.Centimeter + "</option>" +
                                                                            "<option value='point'>" + _Language.DocProperties.Point + "</option>" +
                                                                            "<option value='pica'>" + _Language.DocProperties.Pica + "</option>" +
                                                                        "</select>" +
                                                                    "</div>" +
                                                                    "<div class='sp'><span class='left'>"+_Language.DocProperties.NumOfPages+"</span><span id='cnttxt'></span></div>" +
                                                                "</div>" +
                                                                "<div class='fwr_buttons_bar'><button id='btnconclose' class='fwr_button orange' name='Ok' type='button'>" + _Language.DocProperties.OKBtnTitle + "</button></div>" +
                                                            "</div>" +
                                                    "</div>" +
                                                "</div>";

                var jqDocInfoDlgHtml = $(_DocInfoDlgHtml);
                _PopupDlg = jqDocInfoDlgHtml.dialog({
                    autoOpen: false,
                    modal: true,
                    position: 'center',
                    width: 444,
                    closeText: '',
                    title: _Language.DocProperties.Title
                });
                $("#btnconclose").click(function () {
                    _PopupDlg.dialog("close");
                    return false;
                });
                $("#" + _textID.unit).bind("change", function () {
                    _self.OnChangeUnit();
                });
                _bDlgInited = true;
            }

            if (_PopupDlg) {
                var authorTitle = pDocProperties.GetAuthor();
                if (authorTitle) {
                    $("#" + _textID.author).text(authorTitle);
                    $("#" + _textID.author).attr("title", authorTitle);
                }
                var creatorTitle = pDocProperties.GetCreator();
                if (creatorTitle) {
                    $("#" + _textID.creator).text(creatorTitle);
                    $("#" + _textID.creator).attr("title", creatorTitle);
                }
                var producerTitle = pDocProperties.GetProducer();
                if (producerTitle) {
                    $("#" + _textID.producer).text(producerTitle);
                    $("#" + _textID.producer).attr("title", producerTitle);
                }
                var titleTitle = pDocProperties.GetTitle();
                if (titleTitle) {
                    $("#" + _textID.title).text(titleTitle);
                    $("#" + _textID.title).attr("title", titleTitle);
                }
                var subjectTitle = pDocProperties.GetSubject();
                if (subjectTitle) {
                    $("#" + _textID.subject).text(subjectTitle);
                    $("#" + _textID.subject).attr("title", subjectTitle);
                }
                var keywordsTitle = pDocProperties.GetKeywords();
                if (keywordsTitle) {
                    $("#" + _textID.keywords).val(keywordsTitle);
                }
                var creationDate = pDocProperties.GetCreationDate();
                if (creationDate) {
                    $("#" + _textID.creationData).text(creationDate);
                    $("#" + _textID.creationData).attr("title", creationDate);
                }
                var modifyDate = pDocProperties.GetModifyDate();
                if (modifyDate) {
                    $("#" + _textID.modData).text(modifyDate);
                    $("#" + _textID.modData).attr("title", modifyDate);
                }
                var version = pDocProperties.GetVersion();
                if (version) {
                    $("#" + _textID.version).text(version);
                    $("#" + _textID.version).attr("title", version);
                }
                var nPageCnt = _pReaderApp.GetMainView().GetDocView().GetPageCount();
                $("#" + _textID.pagecount).text(nPageCnt);
                $("#" + _textID.pagecount).attr("title", nPageCnt);
                _self.OnChangeUnit();
                _PopupDlg.dialog('open');
            }
        }


        this.OnChangeUnit = function () {
            var unit = $('#' + _textID.unit).val();
            var iCurPageIndex = _pReaderApp.GetMainView().GetDocView().GetCurPageIndex();
            var curPdfPage = _pReaderApp.GetPDFDoc().GetPage(iCurPageIndex);
            var in_w = _ChangeUnit(unit, curPdfPage.GetPageWidth());
            var in_h = _ChangeUnit(unit, curPdfPage.GetPageHeight());
            var str_size = in_w + " * " + in_h;
            $("#" + _textID.pagesize).text(str_size);
            $("#" + _textID.pagesize).attr("title", str_size);
        }

        this.CreateCmdAndBtn = function () {
            _pReaderApp.AddCommandUI({
                UIName: "PopupDocProperties",
                CommandName: 'PopupDocProperties',
                State: false,
                CreateHtmlContentFunc: function () {
                    return "<a id='btndocppt' class='fwr_toolbar_btn fwr_toobar_doc_property_dis fwr_toobar_image_btn' title='" + _Language.DocProperties.Title + "' style='margin-right:2px;' cmd='" + "PopupDocProperties" + "'></a>";
                },
                OnCommandStateChange: function (data) {

                },
                Init: function () {
                    FXREADER.Common.UpdateBtnState("btndocppt", "fwr_toobar_doc_property_nor", "fwr_toobar_doc_property_dis", true);
                }
            });
            _pReaderApp.AddCommand({
                state: true,
                name: "PopupDocProperties",
                exec: function (data) {
                    _self.PopupPropertiesDlg();
                }
            });
            _pReaderApp.AddMenuItem({
                Name: "MenuDocProperties",
                CreateHtmlContentFunc: function () {
                    return "<li  id='cm_doc_ppt' class='icon' menuname='MenuDocProperties'><span class='icon icon1'></span>" + _Language.DocProperties.Title + "</li>";
                },
                OnShow: function () {
                },
                OnSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled"))
                            _self.PopupPropertiesDlg();
                        else
                            return false;
                    }
                }
            });
        }

    }

    exports.Reader_CreatePlugin = function (pReaderApp) {
        var pDocPropertiesPlugin = new FXREADER.CDocPropertiesPlugin(pReaderApp);
        return pDocPropertiesPlugin;
    }
});